/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

/**
 *
 * @author Mantas, Ramūnas, Vilius
 */
public class PagingMechanism {

    private static Boolean isEmptyBlock(int row){
        int i;
        for(i=0; i<Memory.MAX_BLOCKS-1; i++){
            if(Memory.getMemoryFrom(row, i)!=null){
                return false;
            }
        }
        return true;
    }
    /**
     * Metodas surandantis puslapiavimo lentelei vieta atmintyje
     */
    public static void findBlockForPagingTable(){ 
        Integer random;
        while(true){ 
            random = (int)(Math.random()* Memory.MAX_MEMORY);
            if(isEmptyBlock(random)){
                Processor.setPTR(random);
                Memory.setMemoryAt(random, 0, "");
                break;
            }
        }
    }
    /**
     * Metodas surandantis puslapiavimo lentelei vieta atmintyje (stekui)
     */
    public static void findBlockForPagingTableStack(){ 
        Integer random;
        while(true){ 
            random = (int)(Math.random()* Memory.MAX_MEMORY);
            if(isEmptyBlock(random)){
                Processor.setSPTR(random);
                Memory.setMemoryAt(random, 0, "");
                break;
            }
        }
    }
    /**
     * Metodas grazina atminties bloko adresa panaudojant puslapavimo mechanizma
     * ir puslapavimo mechanizmo lenteles duomenis
     * @param row
     * @return
     */
    public static Integer getPagingTableRow(Integer row){
        String value = Memory.getMemoryFrom(Processor.getPTR(), row);
        if(value==null){
            return null;
        }
        else{
            return Integer.parseInt(value);
        }
    }
    
    
    /**
     * Metodas grazina atminties bloko adresa panaudojant puslapavimo mechanizma
     * ir puslapavimo mechanizmo lenteles duomenis
     * @param row
     * @return
     */
    public static Integer getPagingTableRowStack(Integer row){
        String value = Memory.getMemoryFrom(Processor.getSPTR(), row);
        if(value==null){
            return null;
        }
        else{
            return Integer.parseInt(value);
        }
    }
    
    /**
     * Metodas suranda laisva atminties vieta virtualios masinos blokui realioje
     * atmintyje
     * @param blockNumber
     */
    public static void setPagingTable(Integer blockNumber){
        Integer random;
        while(true){ //kol neranda laisvo bloko atmintyje
            random = (int)(Math.random()* Memory.MAX_MEMORY);
            if(isEmptyBlock(random)){
                Memory.setMemoryAt(Processor.getPTR(), blockNumber, random.toString());
                break;
            }
        }
    }
    
    /**
     * Metodas suranda laisva atminties vieta virtualios masinos blokui realioje
     * atmintyje
     * @param blockNumber
     */
    public static void setPagingTableStack(Integer blockNumber){
        Integer random;
        while(true){ //kol neranda laisvo bloko atmintyje
            random = (int)(Math.random()* Memory.MAX_MEMORY);
            if(isEmptyBlock(random)){
                Memory.setMemoryAt(Processor.getSPTR(), blockNumber, random.toString());
                break;
            }
        }
    }
    /**
     * Metodas pavercia virtualu IP adresa i realios masino IP adresa ir atitinkamai
     * nustato procesoriaus registro reiksme
     */
    public static void convertVirtualIPToReal(){
        String virtualIP = VirtualMachine.getIP();
        String realIP;
        Integer virtualMemoryRow = Integer.parseInt(virtualIP.substring(0, 2));

        Integer realMemoryRow = PagingMechanism.getPagingTableRow(virtualMemoryRow);
      
        if(realMemoryRow<10){
            realIP = Processor.convertXtoXX(realMemoryRow)+virtualIP.substring(2, 4);
            Processor.setIP(realIP);
        }
        else{
            realIP = realMemoryRow.toString()+virtualIP.substring(2, 4);
            Processor.setIP(realIP);
        }
    }
    
    public static void convertVirtualSPToReal(){
        String virtualSP = Stack.getSP();
        String realSP;
        Integer virtualMemoryRow = Integer.parseInt(virtualSP.substring(0, 2));

        Integer realMemoryRow = PagingMechanism.getPagingTableRowStack(virtualMemoryRow);
        if(realMemoryRow<10){
            realSP = Processor.convertXtoXX(realMemoryRow)+virtualSP.substring(2, 4);
            Processor.setSP(realSP);
        }
        else{
            realSP = realMemoryRow.toString()+virtualSP.substring(2, 4);
            Processor.setSP(realSP);
        }
    }
}
